<template>
  <div class="searchIndex">
    <!-- 搜索框 -->

    <div class="searchbox">
      <span class="searchicon"> <van-icon name="arrow-left" @click="back" /> </span>
      <van-search v-model="searchValue" placeholder="请输入搜索关键词" @click-input="show = true" />
      <span class="search" @click="handSearch">搜索</span>
    </div>

    <!-- 历史搜索 -->
    <div class="history" v-show="show == true">
      <div class="recent_searches">
        <span>最近搜索</span>
        <!-- 点击删除svg删除全部搜索历史 -->
        <span @click="celarhistory"
          ><svg
            t="1733984369024"
            class="icon"
            viewBox="0 0 1024 1024"
            version="1.1"
            xmlns="http://www.w3.org/2000/svg"
            p-id="14203"
            width="200"
            height="200"
          >
            <path
              d="M768 153.6h230.4a25.6 25.6 0 1 1 0 51.2H921.6v716.8a102.4 102.4 0 0 1-102.4 102.4H204.8a102.4 102.4 0 0 1-102.4-102.4V204.8H25.6a25.6 25.6 0 0 1 0-51.2H256V102.4a102.4 102.4 0 0 1 102.4-102.4h307.2a102.4 102.4 0 0 1 102.4 102.4v51.2z m-51.2 0V102.4a51.2 51.2 0 0 0-51.2-51.2H358.4a51.2 51.2 0 0 0-51.2 51.2v51.2h409.6zM153.6 204.8v716.8a51.2 51.2 0 0 0 51.2 51.2h614.4a51.2 51.2 0 0 0 51.2-51.2V204.8H153.6z"
              fill="#b0b0b0"
              p-id="14204"
            ></path>
          </svg>
        </span>
      </div>

      <button class="historybtn" v-for="(item, index) in historyVal" :key="index">
        <span @click="historySearch(item)">{{ item }}</span>
      </button>
    </div>
    <div v-show="show == false">
      <div v-show="searchlist == ''">
        <van-empty description="没有搜索到相关的内容哦" />
      </div>
      <div v-show="searchlist">
        <div
          class="recommend-list"
          v-for="item in searchlist"
          :key="item.id"
          @click="toDetail(item)"
        >
          <div class="Dtop">
            <img :src="item.avatar" alt="" />
            <div class="Dtop_left">
              <p>{{ item.name }}</p>
              <p style="color: #c0c0c0">
                <span>{{ item.position }}</span
                >&nbsp;<span>{{ item.time }}</span>
              </p>
            </div>
          </div>
          <div class="Dmain" v-show="item.type === '视频'">
            <div class="type1">
              <span class="type" style="background-color: #a0a8f8">{{ item.type }}</span>
              <span class="typebotom">{{ item.title }}</span>
            </div>
            <div class="video">
              <img class="video" v-lazy="item.img" />
            </div>
            <van-text-ellipsis rows="1" :content="item.desc" />
          </div>
          <div class="Dmain" v-show="item.type === '问答'">
            <div class="type1">
              <span class="type" style="background-color: #38d1af">{{ item.type }}</span>
              <span class="typebotom">{{ item.title }}</span>
            </div>
            <van-text-ellipsis rows="1" :content="item.desc" />
          </div>
          <div class="Dmain" v-show="item.type === '音频'">
            <div class="type1">
              <span class="type" style="background-color: #7ed6ee">{{ item.type }}</span>
              <span class="typebotom">{{ item.title }}</span>
            </div>
            <div class="audio">
              <img v-lazy="item.mp3" />
            </div>
            <van-text-ellipsis rows="1" :content="item.desc" />
          </div>
          <div class="Dmain" v-show="item.type === '文章'">
            <div class="type1">
              <span class="type" style="background-color: #43d3b4">{{ item.type }}</span>
              <span class="typebotom">{{ item.title }}</span>
            </div>
            <van-text-ellipsis rows="1" :content="item.desc" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import axios from 'axios'
const searchValue = ref('')
const show = ref(true)
import "./index.css";

// 搜索结果

// 往pinia存搜索框的数据
const historyVal = ref(
  localStorage.getItem('history') ? JSON.parse(localStorage.getItem('history') as string) : []
)
const router = useRouter()
// 点击返回上级
const back = () => {
  router.go(-1)
}
// 点击删除icon图标删除搜索历史全部数据
const celarhistory = () => {
  historyVal.value = []
  localStorage.removeItem('history')
}
// 搜索之后的数据
let searchlist = ref()
const handSearch = () => {
  if (searchValue.value == '') {
    alert('请输入搜索内容')
  } else {
    historyVal.value.unshift(searchValue.value)
    localStorage.setItem('history', JSON.stringify(historyVal.value))
    axios.get('/api/DownDoctorList', { params: { searchValue: searchValue.value } }).then((res) => {
      const { data, length } = res.data
      searchlist.value = data
      show.value = false
    })
    searchValue.value = ''
  }
}
const historySearch = (item) => {
  // 点击搜索历史内容将搜索的数据调用
  axios.get('/api/DownDoctorList', { params: { searchValue: item } }).then((res) => {
    // 将搜索完成之后的数据存到searchVal中
    const { data, length } = res.data
    console.log(res.data.data)
    searchlist.value = data
    show.value = false
  })
}
const toDetail = (item) => {
  const val = encodeURIComponent(JSON.stringify(item))
  router.push(`/recdetail?item=` + val)
}
</script>
<style>
.searchIndex {
  width: 100vw;
  height: 100vh;
  background-color: #f5f5f5;
}
.recent_searches {
  width: 100%;
  height: 30px;
  margin-top: 46px;
  margin-bottom: 15px;
  display: flex;
  line-height: 14px;
  margin-left: 36px;
  font-size: 30px;
  justify-content: space-between;
}

.recent_searches svg {
  width: 36px;
  height: 36px;
  float: right;
  margin-right: 34px;
}
.historybtn {
  border: none;
  background: #e1e1e1;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 20px;
  margin: 0.25rem;
  margin-left: 36px;
}
.historybtn span {
  font-size: 60px;
  margin-left: 32px;
  margin-right: 32px;
  color: #292929;
  font-size: 30px;
}
.searchicon {
  display: inline-block;
  width: 2.125rem;
  height: 2.75rem;
  text-align: center;
  line-height: 2.75rem;
}
.searchbox {
  width: 100%;
  height: 130px;
  background-color: #fff;
  display: flex;
  align-items: center;
}
.search {
  width: 3.125rem;
  height: 2.75rem;
  line-height: 2.75rem;
  text-align: center;
  color: #015dff;
  font-size: 36px;
  font-weight: 700;
}
.van-search__content {
  --van-radius-sm: 17px;
}
.van-search__field {
  width: 500px;
}
.recommend-list {
  width: 100vw;
  position: relative;
  padding-bottom: 24px;
  padding: 3%;
  border-bottom: solid 1px #e8e8e8;
}

.Dtop_left {
  position: absolute;
  top: 55px;
  left: 150px;
}

.Dtop img {
  width: 111px;
  height: 111px;
  border-radius: 111px;
  margin-top: 7px;
}

.type {
  display: inline-block;
  width: 80px;
  height: 40px;
  border-radius: 10px;
  color: #fff;
  font-size: 24px;

  line-height: 44px;
  text-align: center;
}
.type1 {
  margin-bottom: 20px;
}
.typebotom {
  display: inline-block;
  margin-left: 10px;
  font-size: 30px;
}
.video {
  width: 700px;
  height: 400px;
  background-color: yellow;
  border-radius: 20px;
  margin-bottom: 10px;
}
.audio {
  width: 300px;
  height: 30px;
  background-color: rgb(85, 193, 255);
  border-radius: 20px;
  margin-bottom: 10px;
}
.van-text-ellipsis {
  font-size: 26px;
  color: #848484;
}
</style>